package cn.stylefeng.guns.sys.modular.system.controller;

import cn.hutool.core.collection.CollectionUtil;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.sys.modular.system.entity.GroupNoticeTitlePics;
import cn.stylefeng.guns.sys.modular.system.model.params.GroupNoticeParam;
import cn.stylefeng.guns.sys.modular.system.model.params.GroupNoticeReceiveParam;
import cn.stylefeng.guns.sys.modular.system.model.params.GroupNoticeTitlePicsParam;
import cn.stylefeng.guns.sys.modular.system.model.params.UserParam;
import cn.stylefeng.guns.sys.modular.system.model.result.GroupNoticeDeptResult;
import cn.stylefeng.guns.sys.modular.system.model.result.GroupNoticeResult;
import cn.stylefeng.guns.sys.modular.system.model.result.GroupNoticeTitlePicsResult;
import cn.stylefeng.guns.sys.modular.system.service.GroupNoticeReceiveService;
import cn.stylefeng.guns.sys.modular.system.service.GroupNoticeService;
import cn.stylefeng.guns.sys.modular.system.service.GroupNoticeSignsService;
import cn.stylefeng.guns.sys.modular.system.service.GroupNoticeTitlePicsService;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;


/**
 * 控制器
 *
 * @author jiangge
 * @Date 2021-07-26 13:21:06
 */
@Controller
@RequestMapping("/groupNoticeApi")
public class GroupNoticeApiController extends BaseController {
    private Logger logger = LoggerFactory.getLogger(GroupNoticeApiController.class);
    @Autowired
    private GroupNoticeSignsService groupNoticeSignsService;
    @Autowired
    private GroupNoticeService groupNoticeService;
    @Autowired
    private GroupNoticeTitlePicsService groupNoticeTitlePicsService;
    @Autowired
    private GroupNoticeReceiveService groupNoticeReceiveService;

    /**
     * 随机获取一个署名
     *
     * @return
     */
    @RequestMapping(method = RequestMethod.POST, path = "/getOneSign")
    @ResponseBody
    public ResponseData getOneSign() {
        String detail = this.groupNoticeSignsService.getOneSign();
        return ResponseData.success(detail);
    }

    /**
     * 根据手机号获取部门列表
     *
     * @return
     */
    @RequestMapping(method = RequestMethod.POST, path = "/getDeptList")
    @ResponseBody
    public ResponseData getDeptList(@RequestBody UserParam param) {
        if (StringUtils.isBlank(param.getPhone())) {
            return ResponseData.error(412, "请求失败，手机号参数不能为空");
        }

        List<GroupNoticeDeptResult> deptList = this.groupNoticeSignsService.getDeptList(param);
        return ResponseData.success(deptList);
    }

    /**
     * 发布通知
     *
     * @author jiangge
     * @Date 2021-07-26
     */
    @RequestMapping(method = RequestMethod.POST, path = "/addItem")
    @ResponseBody
    public ResponseData addItem(@RequestBody GroupNoticeParam param) {

        if (param != null && param.getNoticeId() != null) {
            logger.info("群通知-更新通知请求参数：{}", param.toString());
        } else {
            logger.info("群通知-发布通知请求参数：{}", param.toString());
        }
        if (StringUtils.isBlank(param.getTitle()) && StringUtils.isBlank(param.getContent())) {
            return ResponseData.error(412, "请求失败，标题和内容不能同时为空");
        }
        if (param.getPortalUserId() == null) {
            return ResponseData.error(412, "请求失败，发布人portalUserId不能为空");
        }
        if (StringUtils.isBlank(param.getPortalUserName())) {
            return ResponseData.error(412, "请求失败，发布人姓名不能为空");
        }

        this.groupNoticeService.add(param);
        return ResponseData.success();
    }

    /**
     * 保存封面图片信息
     *
     * @author jiangge
     * @Date 2021-07-26
     */
    @RequestMapping(method = RequestMethod.POST, path = "/addTitlePic")
    @ResponseBody
    public ResponseData addTitlePic(@RequestBody GroupNoticeTitlePicsParam param) {
        logger.info("群通知-保存封面信息请求参数：{}", param.toString());
        if (StringUtils.isBlank(param.getPicUrl())) {
            return ResponseData.error(412, "请求失败,图片路径不能为空");
        }
        if (param.getPortalUserId() == null) {
            return ResponseData.error(412, "请求失败,添加人的主键id不能为空");
        }
        if (StringUtils.isBlank(param.getPortalUserName())) {
            return ResponseData.error(412, "请求失败,添加人姓名不能为空");
        }

        GroupNoticeTitlePics groupNoticeTitlePics = this.groupNoticeTitlePicsService.add(param);
        return ResponseData.success(groupNoticeTitlePics);
    }

    /**
     * 删除封面图片
     *
     * @author jiangge
     * @Date 2021-07-26
     */
    @RequestMapping(method = RequestMethod.POST, path = "/deleteTitlePic")
    @ResponseBody
    public ResponseData deleteTitlePic(@RequestBody GroupNoticeTitlePicsParam param) {
        logger.info("群通知-删除封面图片请求参数：{}", param.toString());
        //TODO-1 参数校验
        if (param.getTitlePicId() == null) {
            return ResponseData.error(412, "请求失败，封面主键id不可为空");
        }
        if (param.getPortalUserId() == null) {
            return ResponseData.error(412, "请求失败，操作人id不可为空");
        }
//        if (StringUtils.isBlank(param.getPicUrl())) {
//            return ResponseData.error(412, "请求失败，封面路径不可为空");
//        }

//        String path = param.getPicUrl();
//        path = path.replace(addressUrlConfig.getNoticeUrl(), "D:");
//        File fileMemu = new File(path);
//        //TODO-2 判断文件不为null或文件目录存在
//        if (fileMemu == null || !fileMemu.exists()) {
//            return ResponseData.error(412, "文件删除失败,请检查文件路径是否正确");
//        }
//        try {
//            String filePath = path;
//            filePath = filePath.toString();
//            java.io.File myDelFile = new java.io.File(filePath);
//            myDelFile.delete();
//        } catch (Exception e) {
//            System.out.println("删除文件操作出错");
//            e.printStackTrace();
//            return ResponseData.error(412, "删除文件操作出错");
//        }
        //TODO-2 有通知正在使用，不可删除此封面
        GroupNoticeParam param1 = new GroupNoticeParam();
        param1.setTitlePicId(param.getTitlePicId());
        List<GroupNoticeResult> resultList = this.groupNoticeService.findListBySpec(param1);
        if (CollectionUtil.isNotEmpty(resultList)) {
            return ResponseData.error(412, "删除封面失败，当前有通知正在使用");
        }

        ResponseData responseData = this.groupNoticeTitlePicsService.delete(param);
        return responseData;
    }

    /**
     * 封面图片列表
     *
     * @author jiangge
     * @Date 2021-07-26
     */
    @RequestMapping(method = RequestMethod.POST, path = "/titlePicsList")
    @ResponseBody
    public ResponseData titlePicsList(@RequestBody GroupNoticeTitlePicsParam param) {
        logger.info("群通知-封面信息列表请求参数：{}", param.toString());
        List<GroupNoticeTitlePicsResult> groupNoticeTitlePics = this.groupNoticeTitlePicsService.findListByParam(param);
        List<GroupNoticeTitlePicsResult> publicList = new ArrayList<>();
        List<GroupNoticeTitlePicsResult> privateList = new ArrayList<>();
        if (CollectionUtil.isNotEmpty(groupNoticeTitlePics)) {
            for (GroupNoticeTitlePicsResult data : groupNoticeTitlePics) {
                if (data.getType() == 0) {
                    publicList.add(data);
                } else {
                    privateList.add(data);
                }
            }
        }
        TitlePics titlePics = new TitlePics();
        titlePics.setPrivateList(privateList);
        titlePics.setPublicList(publicList);
        return ResponseData.success(titlePics);
    }

    @Data
    private class TitlePics {
        private List<GroupNoticeTitlePicsResult> publicList;
        List<GroupNoticeTitlePicsResult> privateList;
    }

    /**
     * 通知列表
     *
     * @author jiangge
     * @Date 2021-07-08
     */
    @ResponseBody
    @RequestMapping(value = "/findNoticePageData", method = RequestMethod.POST)
    public ResponseData findNoticePageData(GroupNoticeParam param) {
        logger.info("群通知-通知列表请求参数：{}", param.toString());
        if (param.getDataType() == null) {
            return ResponseData.error(412, "请求失败，列表类型参数不可为空");
        }
        if (param.getDataType() != 0 && param.getDataType() != 1 && param.getDataType() != 2) {
            return ResponseData.error(412, "请求失败，列表类型参数只能为0或1");
        }
        if (param.getDataType() == 2 && param.getNoticeId() == null) {
            return ResponseData.error(412, "请求失败，群通知主键id,不可为空");
        }
        if (param.getDataType() != 2 && param.getPortalUserId() == null) {
            return ResponseData.error(412, "请求失败，当前登录者参数portalUserId不能为空");
        }
        if (param.getLimit() == null) {
            return ResponseData.error(412, "请求失败，分页参数-每页数量为必传，不可为空");
        }
        if (param.getPage() == null) {
            return ResponseData.error(412, "请求失败，分页参数-当前页为必传，不可为空");
        }


        //0:发布 1：接收 2：通知查看情况
        LayuiPageInfo pageInfo = null;
        if (param.getDataType() == 0) {
            pageInfo = this.groupNoticeService.findNoticePagePushData(param);
            return ResponseData.success(pageInfo);
        } else if (param.getDataType() == 1) {
            pageInfo = this.groupNoticeService.findNoticePageReceiveData(param);
        } else if (param.getDataType() == 2) {
            pageInfo = this.groupNoticeService.findNoticePageBeenSeenData(param);
        }
        return ResponseData.success(pageInfo);
    }

    /**
     * 获取通知详情
     *
     * @param param
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/findOneNoticeById", method = RequestMethod.POST)
    public ResponseData findOneNoticeById(@RequestBody GroupNoticeParam param) {
        logger.info("群通知-获取通知详情请求参数：{}", param.toString());
        if (param.getNoticeId() == null) {
            return ResponseData.error(412, "请求失败，通知主键id不可为空");
        }
        GroupNoticeResult result = this.groupNoticeService.findOneNoticeById(param);
        return ResponseData.success(result);
    }

    /**
     * 保存查看/接收通知信息
     *
     * @param param
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/saveNoticeReceicedInfo", method = RequestMethod.POST)
    public ResponseData saveNoticeReceicedInfo(@RequestBody GroupNoticeReceiveParam param) {
        logger.info("群通知-接收通知信息请求参数：{}", param.toString());
        if (param.getNoticeId() == null) {
            return ResponseData.error(412, "请求失败，通知主键id不可为空");
        }
        if (param.getPortalUserId() == null) {
            return ResponseData.error(412, "请求失败，操作人id不可为空");
        }
        if (param.getPortalUserName() == null) {
            return ResponseData.error(412, "请求失败，操作人姓名不可为空");
        }

        ResponseData result = this.groupNoticeReceiveService.saveNoticeReceicedInfo(param);
        return result;
    }

    /**
     * 删除已发布的通知
     *
     * @param param
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/delNotice", method = RequestMethod.POST)
    public ResponseData delNotice(@RequestBody GroupNoticeParam param) {
        logger.info("群通知-删除已发布的通知请求参数：{}", param.toString());
        if (param.getNoticeId() == null) {
            return ResponseData.error(412, "请求失败，通知主键id不可为空");
        }
        if (param.getPortalUserId() == null) {
            return ResponseData.error(412, "请求失败，操作人id不可为空");
        }
        if (param.getPortalUserName() == null) {
            return ResponseData.error(412, "请求失败，操作人姓名不可为空");
        }
        ResponseData result = this.groupNoticeService.delNotice(param);
        return result;
    }
}


